package com.future.mayi.fragment;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.activity.BaseFragment;
import com.future.mayi.activity.RegisterActivity;
import com.future.mayi.model.PhoneAreaModel;
import com.future.mayi.response.PhoneAreaResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterPhoneFragment extends BaseFragment {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.edt_phones)
    EditText edtPhones;
    @BindView(R.id.edt_invite)
    EditText edtInvite;
    @BindView(R.id.btn_enter)
    Button btnEnter;
    @BindView(R.id.view_invite)
    View viewInvite;
    @BindView(R.id.tv_register)
    TextView tvRegister;

    @BindView(R.id.tv_area)
    TextView tvArea;

    private int type;

    private String registerPhone;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_register_phone;
    }

    @Override
    protected void initView() {
        setType(type);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        getArea();
        CommonUtils.showKeyboard(edtPhones);
    }


    public void setEnabled(boolean clickable) {
        if (btnEnter != null)
            btnEnter.setEnabled(clickable);
    }

    public void setTime(int time) {
        if (btnEnter == null) return;
        if (time == 0)
            btnEnter.setText("获取验证码");
        else
            btnEnter.setText(time + "s后获取验证码");
    }


    public void setType(int type) {
        this.type = type;
        if (tvTitle == null) return;
        tvTitle.setText(type == 3 ? "绑定手机号" : (type == 2 ? "注册" : "手机号登录"));
        tvHint.setVisibility(type == 3 ? View.VISIBLE : View.GONE);
        edtInvite.setVisibility(type == 1 ? View.GONE : View.VISIBLE);
        viewInvite.setVisibility(type == 1 ? View.GONE : View.VISIBLE);
        tvRegister.setVisibility(type == 1 ? View.VISIBLE : View.GONE);
        if (type == 3)
            edtPhones.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    Editable editable = edtPhones.getText();
                    if ((!TextUtils.isEmpty(editable)) && editable.toString().length() > 5) {
                        ((RegisterActivity) getActivity()).checkPhoneIsRegister(editable.toString(), getCountryId(), getCountryCode());
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
    }


    @OnClick({R.id.btn_enter, R.id.tv_register, R.id.tv_area})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_enter: {
                String phone = edtPhones.getText().toString();
                String code = edtInvite.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    toast("请输入手机号");
                    return;
                }
                if (type != 1 && TextUtils.isEmpty(code) && (!TextUtils.equals(registerPhone, phone))) {
                    toast("请输入邀请码");
                    return;
                }
                ((RegisterActivity) getActivity()).enter(phone, code, getCountryId(), getCountryCode());
            }
            break;
            case R.id.tv_register: {
                RegisterActivity.startActivity(getActivity(), 2);
            }
            break;
            case R.id.tv_area: {
                CommonUtils.hideKeyboard(null, getActivity());
                selectArea();
            }
            break;
        }
    }

    public String getPhone() {
        return TextUtils.isEmpty(edtPhones.getText()) ? "" : edtPhones.getText().toString();
    }


    public void setRegisterPhone(String registerPhone) {
        this.registerPhone = registerPhone;
    }


    private List<String> phoneAreaList;
    private OptionsPickerView phoneAreaDialog;
    public List<PhoneAreaModel> phoneAreaModels;
    private PhoneAreaModel thisAreModel;

    private void getArea() {
        new HttpRequest<PhoneAreaResponse>()
                .with(mContext)
                .setApiCode(ApiCst.getCountry)
                .setListener(new HttpRequest.OnNetworkListener<PhoneAreaResponse>() {
                    @Override
                    public void onSuccess(PhoneAreaResponse baseResponse) {
                        phoneAreaModels = baseResponse.list;
                        if (phoneAreaModels == null || phoneAreaModels.size() <= 0) return;
                        phoneAreaList = new ArrayList<>();
                        for (PhoneAreaModel model : phoneAreaModels) {
                            if (model == null) continue;
                            phoneAreaList.add(model.getCountry_name() + " +" + model.getArea_code());
                        }
                    }

                    @Override
                    public void onFail(String message, PhoneAreaResponse response) {

                    }
                }).start(new PhoneAreaResponse());
    }

    private void selectArea() {
        if (phoneAreaList == null) {
            getArea();
            toast("正在获取区号");
            return;
        }
        if (phoneAreaDialog == null) {
            phoneAreaDialog = new OptionsPickerView.Builder(mContext, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    if (phoneAreaModels == null || phoneAreaModels.size() <= options1) return;
                    thisAreModel = phoneAreaModels.get(options1);
                    tvArea.setText("+" + thisAreModel.getArea_code());
                }
            }).setCancelColor(getResources().getColor(R.color.main_text_color))
                    .setSubmitColor(getResources().getColor(R.color.main_color))
                    .setContentTextSize(20)
                    .setTitleText("请选择国家")
                    .build();
            phoneAreaDialog.setPicker(phoneAreaList);
        }
        phoneAreaDialog.show();
    }

    public String getCountryId() {
        return thisAreModel == null ? null : String.valueOf(thisAreModel.getCountry_id());
    }

    public String getCountryCode() {
        return thisAreModel == null ? null : thisAreModel.getArea_code();
    }
}
